package com.mdd.admin.controller.system;

import com.mdd.admin.config.aop.Log;
import com.mdd.admin.service.system.ICouponService;
import com.mdd.admin.validate.system.CouponParam;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.system.CouponProvideParam;
import com.mdd.admin.vo.system.CouponListVo;
import com.mdd.admin.vo.system.CouponDetailVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 优惠券管理
 * @author panweiliang
 */
@RestController
@RequestMapping("api/system/coupon")
public class CouponController {

    @Resource
    ICouponService iCouponService;

    /**
     * 优惠券列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return Object
     */
    @GetMapping("/available")
    public Object available(@Validated PageParam pageParam,
                       @RequestParam Map<String, String> params) {
        PageResult<CouponListVo> list = iCouponService.listAvailable(pageParam, params);
        return AjaxResult.success(list);
    }

    /**
     * 优惠券列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return Object
     */
    @GetMapping("/list")
    public Object list(@Validated PageParam pageParam,
                       @RequestParam Map<String, String> params) {
        PageResult<CouponListVo> list = iCouponService.list(pageParam, params);
        return AjaxResult.success(list);
    }

    /**
     * 优惠券详情
     *
     * @author panweiliang
     * @param id 主键ID
     * @return Object
     */
    @GetMapping("/detail")
    public Object detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        CouponDetailVo detail = iCouponService.detail(id);
        return AjaxResult.success(detail);
    }

    /**
     * 优惠券新增
     *
     * @author panweiliang
     * @param couponParam 参数
     * @return Object
     */
    @Log(title = "优惠券新增")
    @PostMapping("/add")
    public Object add(@Validated(value = CouponParam.create.class) @RequestBody CouponParam couponParam) {
        iCouponService.add(couponParam);
        return AjaxResult.success();
    }

    /**
     * 发放优惠券
     *
     * @author panweiliang
     * @param couponParam 参数
     * @return Object
     */
    @Log(title = "发放优惠券")
    @PostMapping("/provide")
    public Object provide(@Validated(value = CouponProvideParam.create.class) @RequestBody CouponProvideParam couponProvideParam) {
        iCouponService.provide(couponProvideParam);
        return AjaxResult.success();
    }



    /**
     * 优惠券编辑
     *
     * @author panweiliang
     * @param couponParam 参数
     * @return Object
     */
    @Log(title = "优惠券编辑")
    @PostMapping("/edit")
    public Object edit(@Validated(value = CouponParam.update.class) @RequestBody CouponParam couponParam) {
        iCouponService.edit(couponParam);
        return AjaxResult.success();
    }

    /**
     * 优惠券删除
     *
     * @author panweiliang
     * @param couponParam 参数
     * @return Object
     */
    @Log(title = "优惠券删除")
    @PostMapping("/del")
    public Object del(@Validated(value = CouponParam.delete.class) @RequestBody CouponParam couponParam) {
        iCouponService.del(couponParam.getId());
        return AjaxResult.success();
    }

    /**
     * 发放优惠券
     *
     * @author panweiliang
     * @return AjaxResult
     */
    @Log(title = "发放优惠券")
    @PostMapping("/enable")
    public AjaxResult enable(@RequestBody CouponParam couponParam) {
        return AjaxResult.success(iCouponService.enable(couponParam.getId()));
    }

    /**
     * 结束发放优惠券
     *
     * @author panweiliang
     * @param id 优惠券id
     * @return AjaxResult
     */
    @Log(title = "结束发放优惠券")
    @PostMapping("/disable")
    public AjaxResult disable(@RequestBody CouponParam couponParam) {
        return AjaxResult.success(iCouponService.disable(couponParam.getId()));
    }

    /**
     * 优惠券不同状态个数
     *
     * @author panweiliang
     * @return Object
     */
    @GetMapping("/count")
    public Object count() {
        Map<String, Object> map = iCouponService.count();
        return AjaxResult.success(map);
    }

}
